<?php
if ($_POST) {
    header("Content-type:text/html;charset=utf-8");
    include '../../api/config.php';
    include './comm.php';
    $arr = array();
    //数据库配置文件
    $json = $_POST["data"];
    $data = json_decode($json);
    $id = $data->head;

    if ($id == '1004') {
        $name = $data->name;
        $flag = $data->flag;
        $indexs = $data->indexs;
        $ids = $data->ids;
        $type = $data->type;
        $href = $data->href;
        //查询用户
        if ($type == '1') {
            $sql2 = "UPDATE admin_nav SET name='{$name}',flag='{$flag}',indexs='{$indexs}',href='{$href}' WHERE id='{$ids}'";
            //更新顶级菜单
        } else {
            $sql2 = "UPDATE admin_nav_list SET name='{$name}',flag='{$flag}',indexs='{$indexs}',href='{$href}' WHERE id='{$ids}'";
            //更新下级菜单
        }
        if(flag($data)){
            $stmt_1 = $conn->prepare($sql2);
            $stmt_1->execute();
            $result_1 = $stmt_1->setFetchMode(PDO::FETCH_ASSOC);
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }
    //更新菜单

    if ($id == '1005') {
        $name = $data->name;
        $flag = $data->flag;
        $indexs = $data->indexs;
        $href = $data->href;
        $sql3 = "INSERT INTO admin_nav_list(name,indexs,href,flag) VALUES('{$name}','{$indexs}','{$href}','{$flag}')";
        if(flag($data)){
            $stmt_2 = $conn->prepare($sql3);
            $stmt_2->execute();
            $result_2 = $stmt_2->setFetchMode(PDO::FETCH_ASSOC);
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }
    //新建下级菜单
    
    if ($id == '1006') {
        $ids = $data->ids;
        $sql4 = "DELETE FROM admin_nav WHERE id = '{$ids}'";
        if(flag($data)){
            $stmt_3 = $conn->prepare($sql4);
            $stmt_3->execute();
            $result_3 = $stmt_3->setFetchMode(PDO::FETCH_ASSOC);
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };

    }
    //删除顶级菜单
    if ($id == '1007') {
        $ids = $data->ids;
        $sql5 = "DELETE FROM admin_nav_list WHERE id = '{$ids}'";
        if(flag($data)){
            $stmt_4 = $conn->prepare($sql5);
            $stmt_4->execute();
            $result_4 = $stmt_4->setFetchMode(PDO::FETCH_ASSOC);
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };

    }
    //删除下级菜单
    if ($id == '1008') {
        $name = $data->name;
        $flag = $data->flag;
        $indexs = $data->indexs;
        $href = $data->href;
        $sql6 = "INSERT INTO admin_nav(name,indexs,href,flag) VALUES('{$name}','{$indexs}','{$href}','{$flag}')";
        if(flag($data)){
            $stmt_5 = $conn->prepare($sql6);
            $stmt_5->execute();
            $result_5 = $stmt_5->setFetchMode(PDO::FETCH_ASSOC);
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };

    }
    //新建顶级菜单

    // // 输入内容
    // if (flag($data)) {
    //     $arr['code'] = '200';
    //     $arr['msg'] = '操作成功~';
    //     $data = json_encode($arr);
    //     echo $data;
    // } else {
    //     echo get_msg('500','权限不足~','../index.html',false);
    // }
    // // 输出内容
    $conn = null;
} else {
    echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
}